﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ComUIControl
{
    public partial class PhotoDisplay : UserControl
    {
        private int _photosCount = 0;
        private int _pageCount = 0;
        private List<Bitmap> listImages = new List<Bitmap>();
        public PhotoDisplay(List<Bitmap> list)
        {
            InitializeComponent();
            SetPhotos(list);
        }

        public PhotoDisplay()
        {
            InitializeComponent();
        }

        public void SetPhotos(List<Bitmap> list)
        {
            listImages = list;
            _photosCount = list.Count();
            if (_photosCount <= 3)
            {
                _pageCount = 1;
            }
            else if (_photosCount > 3 && _photosCount <= 6)
            {
                _pageCount = 2;
            }
            else if (_photosCount > 6 && _photosCount <= 9)
            {
                _pageCount = 3;
            }
        }
        private void InitPictureBoxs()
        {
            switch (_photosCount)
            {
                case 1:
                    pictureBox2.Image = listImages[0];
                    pictureBox2.Text = "1";
                    pictureBox1.Visible = false;
                    pictureBox3.Visible = false;
                    pictureBox2.Focus();
                    break;
                case 2:
                    pictureBox1.Image = listImages[0];
                    pictureBox2.Image = listImages[1];
                    pictureBox3.Visible = false;
                    break;
                case 3:
                    pictureBox1.Image = listImages[0];
                    pictureBox2.Image = listImages[1];
                    pictureBox3.Image = listImages[2];
                    break;
                case 4:
                    pictureBox1.Image = listImages[0];
                    pictureBox2.Image = listImages[1];
                    pictureBox3.Image = listImages[2];
                    pictureBox4.Image = listImages[3];
                    pictureBox5.Visible = false;
                    pictureBox6.Visible = false;
                    break;
                case 5:
                    pictureBox1.Image = listImages[0];
                    pictureBox2.Image = listImages[1];
                    pictureBox3.Image = listImages[2];
                    pictureBox4.Image = listImages[3];
                    pictureBox5.Image = listImages[4];
                    pictureBox6.Visible = false;
                    break;
                case 6:
                    pictureBox1.Image = listImages[0];
                    pictureBox2.Image = listImages[1];
                    pictureBox3.Image = listImages[2];
                    pictureBox4.Image = listImages[3];
                    pictureBox5.Image = listImages[4];
                    pictureBox6.Image = listImages[5];
                    break;
                case 7:
                    pictureBox1.Image = listImages[0];
                    pictureBox2.Image = listImages[1];
                    pictureBox3.Image = listImages[2];
                    pictureBox4.Image = listImages[3];
                    pictureBox5.Image = listImages[4];
                    pictureBox6.Image = listImages[5];
                    pictureBox7.Image = listImages[6];
                    pictureBox8.Visible = false;
                    pictureBox9.Visible = false;
                    break;
                case 8:
                    pictureBox1.Image = listImages[0];
                    pictureBox2.Image = listImages[1];
                    pictureBox3.Image = listImages[2];
                    pictureBox4.Image = listImages[3];
                    pictureBox5.Image = listImages[4];
                    pictureBox6.Image = listImages[5];
                    pictureBox7.Image = listImages[6];
                    pictureBox8.Image = listImages[7];
                    pictureBox9.Visible = false;
                    break;
                case 9:
                    pictureBox1.Image = listImages[0];
                    pictureBox2.Image = listImages[1];
                    pictureBox3.Image = listImages[2];
                    pictureBox4.Image = listImages[3];
                    pictureBox5.Image = listImages[4];
                    pictureBox6.Image = listImages[5];
                    pictureBox7.Image = listImages[6];
                    pictureBox8.Image = listImages[7];
                    pictureBox9.Image = listImages[8];
                    break;
                default:
                    break;
            }
        }

        private void PhotoDisplay_Load(object sender, EventArgs e)
        {
            int height = 250;
            switch (_pageCount)
            {
                case 1:
                    height = 250;
                    break;
                case 2:
                    height = 492;
                    break;
                case 3:
                    height = 734;
                    break;
                default:
                    height = this.Height;
                    break;
            }
            this.Size = new Size(this.Width, height);
            Control parent = this.Parent;
            if(parent != null)
            {
                this.Location = parent.Location;
            }            
            InitPictureBoxs();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            radioButton2.Checked = true;
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            radioButton3.Checked = true;
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            radioButton4.Checked = true;
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            radioButton5.Checked = true;
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            radioButton6.Checked = true;
        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {
            radioButton7.Checked = true;
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            radioButton8.Checked = true;
        }

        private void pictureBox9_Click(object sender, EventArgs e)
        {
            radioButton9.Checked = true;
        }


        public int GetSelectedIndex()
        {
            if (_photosCount == 1)
            {
                return 0;
            }
            if (radioButton1.Checked)
            {
                return 0;
            }
            if (radioButton2.Checked)
            {
                return 1;
            }
            if (radioButton3.Checked)
            {
                return 2;
            }
            if (radioButton4.Checked)
            {
                return 3;
            }
            if (radioButton5.Checked)
            {
                return 4;
            }
            if (radioButton6.Checked)
            {
                return 5;
            }
            if (radioButton7.Checked)
            {
                return 6;
            }
            if (radioButton8.Checked)
            {
                return 7;
            }
            if (radioButton9.Checked)
            {
                return 8;
            }
            return 0;
        }

    }
}
